package com.gadgets.framework.common.exception;

import com.gadgets.framework.common.constant.ExceptionEnum;
import com.gadgets.framework.common.constant.GadgetsConstant;
import com.gadgets.framework.common.util.I18nMessageUtils;

import java.util.Locale;

/**
 * @author UncleXiu
 * @version V1.0.0
 * @data 2020-09-13
 *
 */
public abstract class GadgetsException extends RuntimeException{

    /**
     * 错误等级
     */
    private int errorLevel;
    /**
     * 错误码
     */
    private String errorCode;

    public GadgetsException(String message, ExceptionEnum exceptionEnum) {
        super(message);
        errorLevel = exceptionEnum.getErrorLevel();
        errorCode = GadgetsConstant.RESP_CODE.NOT_KNOW_CODE;
    }

    public GadgetsException(Throwable cause, ExceptionEnum exceptionEnum) {
        super(cause);
        errorLevel = exceptionEnum.getErrorLevel();
        errorCode = GadgetsConstant.RESP_CODE.NOT_KNOW_CODE;
    }

    public GadgetsException(ExceptionEnum exceptionEnum, String errorCode, Object... args) {
        super(I18nMessageUtils.buildI18nMsgInfo(Locale.getDefault(),errorCode,args));
        this.errorLevel = exceptionEnum.getErrorLevel();
        this.errorCode = errorCode;
    }

    public GadgetsException(Locale locale, ExceptionEnum exceptionEnum, String errorCode, Object... args) {
        super(I18nMessageUtils.buildI18nMsgInfo(locale,errorCode,args));
        this.errorLevel = exceptionEnum.getErrorLevel();
        this.errorCode = errorCode;
    }

    public int getErrorLevel() {
        return errorLevel;
    }

    public String getErrorCode() {
        return errorCode;
    }

    public void setErrorLevel(int errorLevel) {
        this.errorLevel = errorLevel;
    }

    public void setErrorCode(String errorCode) {
        this.errorCode = errorCode;
    }
}
